setting.callback.onClick = getEquip;
$(document).ready(function(){
	/**
	 * 获取列表页面（行为）按钮
	 */
	var authCondition = {
			//userId:$("#index #userId").val(),
			menuCode:curMenu.code,
			drawerCode:行为类型_前端组件,
			authAreaCode:授权分类_设备管理_列表,
			callback:action,
			con:"#listform"}
	findResourcesByUserId(authCondition);
});

/**
 * 获取部门数据
 * @param data
 */
function getEquip(event,treeId,treeNode){
	var id = (treeNode.id);
	if(id.indexOf("system_")!=-1){
		search(page_);
	}else{
		message("请选择系统查询设备");
	}
}

/**
 * 定义分页全局变量
 */
var page_ = new page({search:search,callback:func});

/**
 * 定义容器全局变量
 */
var con = new container({
	datagrid:'#datagrid',
	datagridtoolbar:'#datagrid-toolbar', 
	singleSelect:true});

/**
 * 查询
 * @param page
 */
function search(pg){
	if(pg == undefined || pg == null){
		pg = page_;
	}
	var url = ctx + "/equip/query";
	page_.pageSize = pg.pageSize;
	page_.pageNumber = pg.pageNumber;
	query(url, getCondition(), con, page_);
}

/**
 * 获取查询条件
 * @returns {condition}
 */
function getCondition(){
	
	var organTree = $.fn.zTree.getZTreeObj("organTree");
	var organ = organTree.getSelectedNodes();
	
	var condition = {};
	
	condition.systemId = organ[0].id;
	
	condition.name = $("#listform #name").textbox('getValue');
	
	return condition;
}

/**
 * 回调函数
 */
function func(){
	$("#datagrid").datagrid({onClickRow:function(index,row){
	}});
}

/**
 * 添加
 */
function add(){
	
	var organTree = $.fn.zTree.getZTreeObj("organTree");
	var organ = organTree.getSelectedNodes();
	
	if(organ.length == 0){
		message("请选择机构");
		return;
	}
	
	var id = (organ[0].id);
	if(id.indexOf("system_")==-1){
		message("请选择系统");
		return;
	}

	$('#dialog').dialog({
	    title: '添加',
	    width: 600,
	    height: 500,
	    closed: false,
	    cache: false,
	    modal: true,
	    href:ctx + "/web/data/equip/add.jsp",
	    onClose:function(){
	    	destroy('dialog','#dialogDiv');
	    },
	    onLoad:function(){
	    	getType('');
	    	$("#addform").form('load',{});
	    },
	    buttons: [{
            text:'保存',
            iconCls:'icon-ok',
            handler:function(){
            	var bool = $("#addform").form('enableValidation').form('validate');
            	if(!bool){
            		return;
            	}
            	var data = $("#addform").serializeJson();
            	data.systemId= id.substring(7,id.length);
            	
            	$.ajax({
            		type : "POST",
            		url : ctx + "/equip/create",
            		data :data,
            		dataType : "json",
            		success : function(data) {
            			if(!showMsg(data,"添加成功")){
            				return;
            			}
            			
            			search(page_);
            			
            			$('#dialog').dialog('close');
            		}
            	});
            }
        },{
            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
            	$('#dialog').dialog('close');
            }
        }]
	});
}

/**
 * 修改
 */
function edit(){
	var row = null;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	$('#dialog').dialog({
	    title: '修改',
	    width: 600,
	    height: 400,
	    closed: false,
	    cache: false,
	    modal: true,
	    href:ctx + "/web/data/equip/add.jsp",
	    onClose:function(){
	    	destroy('dialog','#dialogDiv');
	    },
	    onLoad:function(){
	    	var type = row.type;
			$("#addform #id").val(row.id);
			$("#addform").form('load',row);
			
			getType(type);
	    },
	    buttons: [{
	    	text:'保存',
            iconCls:'icon-ok',
            handler:function(){
            	var bool = $("#addform").form('enableValidation').form('validate');
            	if(!bool){
            		return;
            	}
            	var data = $("#addform").serializeJson();
            	
            	$.ajax({
            		type : "POST",
            		url : ctx + "/equip/update",
            		data :data,
            		dataType : "json",
            		success : function(data) {
            			if(!showMsg(data,"修改成功")){
            				return;
            			}
            			
            			search(page_);
            			
            			$('#dialog').dialog('close');
            		}
            	});
            }
        },{
            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
            	$('#dialog').dialog('close');
            }
        }]
	});
}

function deleteEquip(){
	var row = getSelectedRow("#datagrid");
	if(row){
		$.messager.confirm('提示', '确定删除?', function(r){
	        if (r){
	       	 $.ajax({
	      	        url : ctx+"/equip/delete",
	      	        data : {id:row.id},
	      	        dataType : 'json',
	      	        type : 'post',
	      	        success : function (data) {
	      	        	if(!showMsg(data,"删除成功")){
	    					return;
	      	        	}
      					search(page_);
      					$('#dialog').dialog('close');
	      	    }});
	        }
	    });
	}
}


/**
 * 维护
 */
function maintain(){
	var row = null;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	$('#dialog').dialog({
		title: '维护',
		width: 800,
		height: 600,
		closed: false,
		cache: false,
		modal: true,
		href:ctx + "/web/data/equip_maintain/maintain.jsp",
		onClose:function(){
			destroy('dialog','#dialogDiv');
		},
		onLoad:function(){
			$("#maintainEquipId").val(row.id);
		},
		buttons: [{
			text:'取消',
			iconCls:'icon-cancel',
			handler:function(){
				$('#dialog').dialog('close');
			}
		}]
	});
}